package znbkListBj.adapter;

import android.animation.Animator;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.lancooznbk.R;
import znbkListBj.entity.NoteListEntity;
import com.example.lancooznbk.utils.AnimatorHelper;
import com.example.lancooznbk.view.widget.RippleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018\3\16 0016.
 */

public class NotesListAdapter extends BaseRecyclerAdapter<NotesListAdapter.NotesAdapterViewHolder> {

    private List<NoteListEntity.NoteBean> list = new ArrayList<>();
    private final Activity context;
    private OnItemClickListener onItemClickListener;

    private int actionItem;

    private final Interpolator mInterpolator = new LinearInterpolator();
    private int mLastPosition = -1;


    public interface OnItemClickListener{
        void onItemClick(View view, int position);
        void onItemDel(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public NotesListAdapter(Activity context, List<NoteListEntity.NoteBean> favouriteBeanList) {
        this.context = context;
        this.list = favouriteBeanList;
    }

    public NotesListAdapter(Activity context) {
        this.context = context;
    }


    @Override
    public void onBindViewHolder(final NotesAdapterViewHolder holder, final int position, boolean isItem) {

        holder.mFavouriteItem.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView v) {

                if (onItemClickListener != null && list.size() != 0) {//修复数据清除了界面还存在点击的bug
                    actionItem = (Integer) holder.mFavouriteItem.getTag();
                    onItemClickListener.onItemClick(v,actionItem);
                }
            }
        });
        holder.mBtnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null && list.size() != 0) {
                    actionItem = (Integer) holder.mFavouriteItem.getTag();
                    onItemClickListener.onItemDel(holder.mFavouriteItem,actionItem);
                }
            }
        });
        holder.mFavouriteItem.setTag(position);


        NoteListEntity.NoteBean treeInfoEntity = list.get(position);

        /*
        * 单数背景为蓝色，双数背景为绿色
        * */
        if(position%2==0){
            holder.mFavouriteItem.setBackgroundResource(R.mipmap.znbk_notes_item_blue);
        }else{
            holder.mFavouriteItem.setBackgroundResource(R.mipmap.znbk_notes_item_green);
        }


        holder.mTvContent.setText(Html.fromHtml(treeInfoEntity.getContent()));
        holder.mTvTitle.setText("来源: "+Html.fromHtml(treeInfoEntity.getTitle()));

        String strTime = treeInfoEntity.getCreateTime();
        strTime = strTime.replaceAll("-",".");
        String strCollectionTime = String.format(context.getString(R.string.sc_item_collection_time),strTime);
        holder.mTvTime.setText(Html.fromHtml(strCollectionTime));



        int adapterPosition = holder.getAdapterPosition();

        boolean isScrollDown = adapterPosition > mLastPosition;

        for (Animator anim : AnimatorHelper.getRecyclerAnimator(holder.itemView,isScrollDown)) {
            anim.start();
        }

    }





    @Override
    public int getAdapterItemCount() {
        return list.size();
    }

    public List<NoteListEntity.NoteBean>getAdapterItemList(){
        return list;
    }
    @Override
    public NotesAdapterViewHolder getViewHolder(View view) {
        return new NotesAdapterViewHolder(view, false);
    }

    @Override
    public NotesAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v;
        v = LayoutInflater.from(context).inflate(R.layout.item_rv_bj,parent,false);
        return new NotesAdapterViewHolder(v,true);
    }

    class NotesAdapterViewHolder extends RecyclerView.ViewHolder{

        RippleView mFavouriteItem;
        TextView mTvContent;
        TextView mTvTitle;
        TextView mTvTime;
        Button mBtnDel;

        public NotesAdapterViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if (isItem) {
                mFavouriteItem = (RippleView) itemView.findViewById(R.id.ripple_favourite_item);
                mTvContent = (TextView) itemView.findViewById(R.id.tv_Content);
                mTvTitle = (TextView) itemView.findViewById(R.id.tv_Title);
                mTvTime = (TextView) itemView.findViewById(R.id.tv_Time);
                mBtnDel = (Button) itemView.findViewById(R.id.btn_Delete);
            }

        }


    }



    //添加数据
    public void add(List<NoteListEntity.NoteBean> favouriteBeanList){
        //int ss = getStart();
        int startPosition = getStart()+getAdapterItemCount();
        // int endPosition = startPosition+treeInfoEntityList.size()-1;
        list.addAll(favouriteBeanList);
        notifyItemRangeInserted(startPosition, favouriteBeanList.size());
    }

    public void clearAndAdd(List<NoteListEntity.NoteBean> favouriteBeanList){
        list.clear();
        list.addAll(favouriteBeanList);
        notifyDataSetChanged();
        mLastPosition = -1;
    }




    public void remove(int position) {
        list.remove(position);
        remove(list, position);
    }

    public void remove(){
        list.remove(actionItem);
        //notifyDataSetChanged();
        notifyItemRemoved(actionItem);
        notifyItemRangeChanged(actionItem,getItemCount());


    }

    public void clear() {
        list.clear();//清空不刷新
        //clear(list);//清空刷新
    }


}